/*
 * GameBoardView.java
 *
 * Created on 11 ספטמבר 2009, 19:28
 */

package gomoku;

import gomoku.Controller.GoMokuGame;
import java.awt.Color;
import java.awt.Point;
import gomoku.Model.*;
import java.util.LinkedList;

public class GameBoardView extends javax.swing.JPanel {

    BoardCell boardCells[][];
    static final int CELL_SIZE = 30;
    int rows=15, cols=15;
    
  //  GoMokuEvents gameEvent = null;
            
    /** Creates new form GameBoardView */
   
      public GameBoardView() {
        
        initComponents();
        boardCells = new BoardCell[rows+1][cols+1];
      }            
      
      public void initBoard(LinkedList<GoMokuActionListener> gomokuEventslisteners) {
          
          for (int i = 0 ; i < cols+1; ++i) {
                for (int j = 0 ; j < rows+1; ++j) {
                    BoardCell c = new BoardCell(i*j==0?true:false,i,j);
                    c.setSize(CELL_SIZE, CELL_SIZE);
                    c.setLocation(i*CELL_SIZE + 1, j*CELL_SIZE + 1);
                    if (i*j ==0) {
                        c.setBgColor(new Color(0,150,255));
                    } else {

                        c.addMouseListener(new CellAdapter(gomokuEventslisteners, new Point(i,j)));

                        if (i %2 ==0) {
                            c.setBgColor(Color.GREEN);
                        } else {
                        c.setBgColor(new Color(0,200,0));
                        }
                    }

                    c.updateUI();
                    add(c);
                    boardCells[i][j] = c;
                }
            }
      }

    public void resetBoard()
    {
        for (int i = 1 ; i < cols+1; ++i)
        {
            for (int j = 1 ; j < rows+1; ++j)
            {
                BoardCell c = boardCells[i][j];

                if (i %2 ==0)
                {
                    c.setBgColor(Color.GREEN);
                }
                else
                {
                    c.setBgColor(new Color(0,200,0));
                }

                c.updateUI();
            }
        }
    }

      public void updateBoardView(GoMokuGame game)
      {
          GameBoard board = game.getGameBoard();
          for (int i = 0; i < cols+1; i++)
          {
              for (int j=0; j<rows+1; j++)
              {
                  if (board.getPawnType(i, j) != null)
                  {
                      Color c = board.getPawnType(i, j) == PawnType.Black ? Color.black : Color.white;
                      this.boardCells[j][i].setBgColor(c);
                      this.boardCells[j][i].updateUI();
                  }
              }
          }
      }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 496, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 496, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
